package cn.meibang.common.beauty.provider.dao;


import cn.meibang.common.beauty.provider.domain.BeautyConsultationOrderUserAnswer;

import java.util.Map;

import static org.apache.ibatis.jdbc.SqlBuilder.*;

public class BeautyConsultationOrderUserAnswerSqlProvider {

    public String selectLatest(Map params) {
        Object userId = params.get("userId");
        Object openid = params.get("openid");
        StringBuilder sqlSb = new StringBuilder();
        sqlSb.append("select ");
        sqlSb.append("id, tag, user_id, openid, order_id, question_id, answer_id, create_by, create_date, ");
        sqlSb.append("update_by, update_date, remarks, del_flag, institution_id ");
        sqlSb.append("from beauty_consultation_order_user_answer ");
        sqlSb.append("where order_id = #{orderId} and del_flag = '1' ");
        if (userId != null && (Long) userId > 0) {
            sqlSb.append("and user_id = #{userId} ");
        } else if (openid != null) {
            sqlSb.append("and openid = #{openid} ");
        }
        sqlSb.append("order by id desc ");
        sqlSb.append("limit 1 ");
        return sqlSb.toString();
    }

    public String insertSelective(BeautyConsultationOrderUserAnswer record) {
        BEGIN();
        INSERT_INTO("beauty_consultation_order_user_answer");

        if (record.getId() != null) {
            VALUES("id", "#{id,jdbcType=BIGINT}");
        }

        if (record.getTag() != null) {
            VALUES("tag", "#{tag,jdbcType=VARCHAR}");
        }

        if (record.getUserId() != null) {
            VALUES("user_id", "#{userId,jdbcType=BIGINT}");
        }

        if (record.getOpenid() != null) {
            VALUES("openid", "#{openid}");
        }

        if (record.getOrderId() != null) {
            VALUES("order_id", "#{orderId,jdbcType=BIGINT}");
        }

        if (record.getQuestionId() != null) {
            VALUES("question_id", "#{questionId,jdbcType=BIGINT}");
        }

        if (record.getAnswerId() != null) {
            VALUES("answer_id", "#{answerId,jdbcType=BIGINT}");
        }

        if (record.getCreateBy() != null) {
            VALUES("create_by", "#{createBy,jdbcType=BIGINT}");
        }

        if (record.getCreateDate() != null) {
            VALUES("create_date", "#{createDate,jdbcType=TIMESTAMP}");
        }

        if (record.getUpdateBy() != null) {
            VALUES("update_by", "#{updateBy,jdbcType=BIGINT}");
        }

        if (record.getUpdateDate() != null) {
            VALUES("update_date", "#{updateDate,jdbcType=TIMESTAMP}");
        }

        if (record.getRemarks() != null) {
            VALUES("remarks", "#{remarks,jdbcType=VARCHAR}");
        }

        if (record.getDelFlag() != null) {
            VALUES("del_flag", "#{delFlag,jdbcType=CHAR}");
        }

        if (record.getInstitutionId() != null) {
            VALUES("institution_id", "#{institutionId,jdbcType=BIGINT}");
        }

        return SQL();
    }

    public String updateByPrimaryKeySelective(BeautyConsultationOrderUserAnswer record) {
        BEGIN();
        UPDATE("beauty_consultation_order_user_answer");

        if (record.getTag() != null) {
            SET("tag = #{tag,jdbcType=VARCHAR}");
        }

        if (record.getUserId() != null) {
            SET("user_id = #{userId,jdbcType=BIGINT}");
        }

        if (record.getOpenid() != null) {
            SET("openid = #{openid}");
        }

        if (record.getOrderId() != null) {
            SET("order_id = #{orderId,jdbcType=BIGINT}");
        }

        if (record.getQuestionId() != null) {
            SET("question_id = #{questionId,jdbcType=BIGINT}");
        }

        if (record.getAnswerId() != null) {
            SET("answer_id = #{answerId,jdbcType=BIGINT}");
        }

        if (record.getCreateBy() != null) {
            SET("create_by = #{createBy,jdbcType=BIGINT}");
        }

        if (record.getCreateDate() != null) {
            SET("create_date = #{createDate,jdbcType=TIMESTAMP}");
        }

        if (record.getUpdateBy() != null) {
            SET("update_by = #{updateBy,jdbcType=BIGINT}");
        }

        if (record.getUpdateDate() != null) {
            SET("update_date = #{updateDate,jdbcType=TIMESTAMP}");
        }

        if (record.getRemarks() != null) {
            SET("remarks = #{remarks,jdbcType=VARCHAR}");
        }

        if (record.getDelFlag() != null) {
            SET("del_flag = #{delFlag,jdbcType=CHAR}");
        }

        if (record.getInstitutionId() != null) {
            SET("institution_id = #{institutionId,jdbcType=BIGINT}");
        }

        WHERE("id = #{id,jdbcType=BIGINT}");

        return SQL();
    }
}